﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../BESTSATS.h"
#include "LogHeaderDecoder.h"

namespace GnssLogs
{
namespace BESTSATSDecoder
{
using namespace tao::pegtl;
using Sync   = TAO_PEGTL_STRING("#BESTSATSA");
using Header = LogHeaderDecoder::SyncExpression<Sync>;
// clang-format off
struct Entries : star<digit> {};

struct System : ascii::identifier {};
struct SatelliteID : star<digit> {};
struct Status : ascii::identifier {};
struct SignalMaskRule : star<xdigit> {};
// clang-format on
using SatelliteRule = seq<Split, System, Split, SatelliteID, Split, Status, Split, SignalMaskRule>;

using Expression = seq<Header, Entries, star<SatelliteRule>, one<'*'>, CRC32, Tail>;

template<typename T>
struct Action
{
};

template<>
struct Action<Header> : LogHeaderDecoder::LogHeaderHelperNoSetValue
{
};

template<>
struct Action<Entries> : IntHelper<Action<Entries>, uint32_t>
{
    static void setValue(BESTSATS::Data& data, uint32_t value) { data.entries = value; }
};

template<>
struct Action<System> : EnumHelper<Action<System>, SatelliteSystem>
{
    static void setValue(BESTSATS::Data& data, SatelliteSystem value)
    {
        data.sats.emplace_back(BESTSATS::Data::Satellite {});
        data.sats.back().system = value;
    }
};

template<>
struct Action<SatelliteID> : IntHelper<Action<SatelliteID>, uint32_t>
{
    static void setValue(BESTSATS::Data& data, uint32_t value) { data.sats.back().satelliteID = value; }
};

template<>
struct Action<Status> : EnumHelper<Action<Status>, ObservationStatuses>
{
    static void setValue(BESTSATS::Data& data, ObservationStatuses value) { data.sats.back().status = value; }
};

template<>
struct Action<SignalMaskRule> : IntHelper<Action<SignalMaskRule>, uint32_t, 16>
{
    static void setValue(BESTSATS::Data& data, uint32_t value) { data.sats.back().signalMask = value; }
};

template<>
struct Action<CRC32> : CRC32Helper<Action<CRC32>>
{
    static void setValue(BESTSATS::Data& data, uint32_t value) { data.crc32 = value; }
};
} // namespace BESTSATSDecoder
} // namespace GnssLogs